package sample.spring.enable;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * Created on 2020/7/10
 */
@ImportSelectorSample.EnableHello
public class ImportSelectorSample {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(ImportSelectorSample.class);
		context.refresh();
		System.out.println(context.getBean("hello"));
	}

	@Target(ElementType.TYPE)
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	@Import(HelloImportSelector.class)
	@interface EnableHello {

	}

	static class HelloImportSelector implements ImportSelector {

		public String[] selectImports(AnnotationMetadata importingClassMetadata) {
			return new String[] {"java.lang.String"};
		}
	}

	@Bean
	public String hello() {
		return "hello";
	}
}
